#include "crc8.h"
#include "ui_crc8.h"

crc8::crc8(QWidget *parent) : QDialog(parent), ui(new Ui::crc8) {
  ui->setupUi(this);
  setWindowTitle("累加和校验,累加和模256的值");
  ui->outEdit->setPlainText(
      "1. 645数据帧校验,组帧后直接输出在当前窗口中,\n举例:\n输入:\n  68 AA AA "
      "AA "
      "AA "
      "AA "
      "AA "
      "68 13 00\n输出:\n  68 AA AA AA AA AA AA 68 13 00 df 16\n2. "
      "376.2解析自动计算长度跟校验,然后输出当前窗口,"
      "数据帧长度可以写0自动计算长度\n举例:\n输入:\n  68 00 00 41 00 00 00 00 "
      "00 00 01 01 00 \n输出:\n  68 0F 00 41 00 00 00 00 00 00 01 01 00 43 "
      "16\n3. 698数据帧校验(使用了固定长度域进行计算)\n举例:\n输入:\n  68 00 "
      "00 43 05 01 00 00 00 00 00 "
      "00 a7 e6 05 01 01 00 00 02 00 00 00 00 16\n输出:\n  68 17 00 43 05 01 "
      "00 00 00 00 00 00 A7 E6 05 01 01 00 00 02 00 00 5F DA 16 ");
}

crc8::~crc8() { delete ui; }

void crc8::on_d645Btn_clicked() {
  QString d645 = ui->d645Edit->text().simplified().remove(QRegExp("\\s"));
  QByteArray fData;
  QString sRet;
  if (d645.isEmpty()) {
    QMessageBox::information(this, "警告", "请输入有效数据帧");
    return;
  }
  if (d645.length() < 12) {
    QMessageBox::information(this, "警告", "输入数据帧长度有无");
    return;
  }
  fData = pubFun.QString2Hex(d645);

  fData.append(pubFun.CheckSum(fData));
  sRet = QString("645校验结果:\n  %1")
             .arg(pubFun.hex2QString(fData) + QString("16"));
  ui->outEdit->append(sRet);
}

void crc8::on_d3762Btn_clicked() {
  QString d3762 = ui->dl3762Edit->text().simplified().remove(QRegExp("\\s"));
  QByteArray fData, cData;
  u8 *pIndex = NULL;
  QString sRet;
  u16 len;
  if (d3762.isEmpty()) {
    QMessageBox::information(this, "警告", "请输入有效数据帧");
    return;
  }
  if (d3762.length() < 12) {
    QMessageBox::information(this, "警告", "输入数据帧长度有无");
    return;
  }
  fData = pubFun.QString2Hex(d3762);
  /* 计算长度 */
  len = fData.length() + 2 /* crc ,end 16 */;
  pIndex = (u8 *)&len;
  fData[1] = *pIndex;
  pIndex++;
  fData[2] = *pIndex;

  cData = fData.right(fData.length() - 3);

  /* crc计算去掉三个字节 68 跟 len */
  fData.append(pubFun.CheckSum(cData));
  sRet = QString("376.2校验结果:\n  %1")
             .arg(pubFun.hex2QString(fData) + QString("16"));

  ui->outEdit->append(sRet);
}

void crc8::on_d698Btn_clicked() {
  QString sRet;
  if (ui->dl698Edit->text().isEmpty() == true)
    return;
  char cs[2] = {0};
  char len[2] = {0};
  QString str = ui->dl698Edit->text().simplified().remove(QRegExp("\\s"));
  QByteArray da = pubFun.QString2Hex(str);
  u16 le = da.length() - 2;
  GQ_IOT_Memcpy(len, &le, 2);
  da.replace(1, 2, len, 2);

  QByteArray hcs = da.left(12);
  QByteArray data = hcs.right(11);
  u16 cscs = pubFun.checkpppcs16((byte *)data.data(), data.length());
  GQ_IOT_Memcpy(cs, (u8 *)&cscs, 2);
  da.replace(12, 2, cs, 2);
  hcs.clear();
  hcs = da.left(da.length() - 3);
  data.clear();
  data = hcs.right(hcs.length() - 1);
  cscs = pubFun.checkpppcs16((byte *)data.data(), data.length());
  GQ_IOT_Memcpy(cs, (u8 *)&cscs, 2);
  da.replace(da.length() - 3, 2, cs, 2);

  sRet = QString("698校验结果:\n  %1").arg(pubFun.hex2QString(da));

  ui->outEdit->append(sRet);
}
